import esbuild from 'esbuild'
import swc from '@swc/core'
import fs from 'node:fs'
await esbuild.build({
  entryPoints: ['./index.ts'], //入口文件
  outdir: "dist",
  bundle: true, //模块单独打包
  treeShaking: true,

  define: {
    'process.env.NODE_ENV': '"production"',
  },

  loader: {
    '.js': 'js',
    '.ts': 'ts',
    '.jsx': 'jsx',
    '.tsx': 'tsx',
  },
  plugins: [
    {
      // 实现自定义 loader
      name: "swc-loader",
      setup(build) {
        build.onLoad({ filter: /\.(js|ts|tsx|jsx)$/ }, (args) => {
          // args 是当前加载文件的相关信息
          const content = fs.readFileSync(args.path, "utf-8")
          // 使用 swc 进行语法转化
          const { code } = swc.transformSync(content, {
            filename: args.path
          })
          return {
            contents: code
          }
        })
      },
    }
  ]
})